import {observable,action} from 'mobx';
import {message} from 'antd';
import ajx from '../../utils/ajx';
import apiConf from '../../conf/apiConf';
import routerConf from '../../conf/routerConf';
import history from '../../utils/history';
import publicModel from '../../publicModel/publicModel';
const api = apiConf.user;
class Model {
    @observable username = '';
    @action setUsername = (data) => {
        this.username = data;
    }
    @observable password = '';
    @action setPassword = (data) => {
        this.password = data;
    }

    @observable password1 = '';
    @action setPassword1 = (data) => {
        this.password1 = data;
    }

    @action setEmpty = () => {
        this.username = '';
        this.password = '';
        this.password1 = '';
    }

    @action reg = async () => {
        if (!this.username) {
            message.warning('用户名不可为空！');
            return;
        }
        if (!this.password) {
            message.warning('用户密码不可为空！');
            return;
        }
        if (this.password !== this.password1) {
            message.warning('两次密码输入不一致，请重新输入密码！');
            return;
        }
        ajx.post({
            url:api.register,
            data:{
                username:this.username,
                password:this.password
            }
        }).then(async (res)=>{
            if (res && res.statusCode === 200) {
                publicModel.setUserInfo(res.data);
                message.success('注册成功！');
                setTimeout(()=>{
                    history.push(routerConf.overView.path);
                },3000);
            }else if (res && res.statusCode === 500) {
                message.warning(res.error);
            }
        } ).catch((err)=>{
            message.warning('未知错误！');
        })
    }
}

export default new Model();